home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 1998 June / SGI IRIX 6.5 Applications 1998 June.iso / dist / outbox.idb / var / www / cgi-bin / handler.z / handler
Text File  |  1998-05-04  |  4KB  |  190 lines

  1. #!/usr/bin/perl
  2.  
  3. #__________________________________________________________
  4. #
  5. #   File:    handler
  6. #   By:      Matt Ho
  7. #   Date:    7/23/95
  8. #   Purpose: Appropriately packages documents for download
  9. #            or display.
  10. #__________________________________________________________
  11.  
  12.  
  13. #__________________________________________________________
  14. #
  15. #   IRIX 6.5 disables the handler script for security
  16. #   reasons. This script will be removed entirely in the
  17. #   next release.
  18. #
  19. #   If you need to restore its functionality, and you are
  20. #   behind a firewall, and trust all others behind that
  21. #   firewall, then change the line below to:
  22. #        $HANDLER_DISABLED = 0 ;
  23. #__________________________________________________________
  24.  
  25. $HANDLER_DISABLED = 1 ;
  26.  
  27.  
  28. #__________________________________________________________
  29. #
  30. #   If handler script is disabled, display a message and
  31. #   exit.
  32. #__________________________________________________________
  33.  
  34. if ( $HANDLER_DISABLED != 0 ) {
  35.     print <<ENDOFTEXT ;
  36. Content-type: text/html
  37.  
  38. <HTML>
  39. <HEAD><TITLE>OutBox: Download feature disabled.</TITLE></HEAD>
  40. <BODY><H2>OutBox: Download feature disabled.</H2>
  41. To download a file from an OutBox page, use your web browser's
  42. 'Save Link' feature. (In Netscape Navigator move the mouse over the file
  43. and select 'Save Link As' from the popup menu on mouse button 3, or use
  44. the mouse accelerator, "Shift-Button1")
  45. </BODY>
  46. </HTML>
  47. ENDOFTEXT
  48.  
  49.     exit ;
  50. }
  51.  
  52. #__________________________________________________________
  53. #
  54. #    Set some environment variables, we'll need through the
  55. #    script and do some initial error checking.
  56. #__________________________________________________________
  57.  
  58. $ROOT     = "/var/www/htdocs" ;    # Root directory
  59. $PATH     = $ENV{'PATH_INFO'} ;
  60.  
  61. # trim off undesirable meta chars.
  62. $PATH =~ s/[|;]//g ;
  63.  
  64. chop $PATH if substr($PATH, -1) eq "/" ;
  65. @_        = split('/', $PATH) ;
  66. $pathRoot = $_[$#_] ;
  67. $doc      = $ROOT.$PATH ;
  68.  
  69. &ErrBadPath unless -f $doc ;
  70. &ErrBadPath unless &ValidPath ; # Check for server spoofing
  71.  
  72. #__________________________________________________________
  73. #
  74. #    Read the form data in (we just may need this)
  75. #__________________________________________________________
  76.  
  77. if( $ENV{'REQUEST_METHOD'} eq "GET" )
  78. {
  79.    $buffer=$ENV{'QUERY_STRING'} ;
  80. else
  81. {
  82.    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}) ;
  83. }
  84.  
  85. @pairs = split(/&/, $buffer) ;
  86. foreach (@pairs)
  87. {
  88.     tr/+/ / ;    
  89.    ($name,$value) =  split(/=/) ;
  90.     $value        =~ s/%(..)/pack("c",hex($1))/ge ;
  91.     $name         =~ s/%(..)/pack("c",hex($1))/ge ;
  92.  
  93.     $FORM{$name} = $value ;
  94. }
  95.  
  96.  
  97. #__________________________________________________________
  98. #
  99.  
  100. $data = $FORM{'data'} ;
  101. if( $data eq "Download" )
  102. {
  103.     unless( open(INPUT, $doc) )
  104.     {
  105.         print <<ENDOFTEXT ;
  106. Content-type: text/html
  107.  
  108. <HEAD><TITLE>404 Not Found</TITLE></HEAD>
  109. <BODY><H1>404 Not Found</H1>
  110. The requested URL was not found on this server: $ENV{'PATH_INFO'}
  111. <P>
  112. </BODY>
  113. ENDOFTEXT
  114.         return ;
  115.     } 
  116.     print <<ENDOFTEXT ;
  117. Content-type: application/octet-stream
  118.  
  119. ENDOFTEXT
  120.  
  121.     while( read(INPUT, $buf, 16384) )
  122.     {
  123.         print $buf ;
  124.     } 
  125.  
  126.     close(INPUT) ;
  127. }
  128. elsif( $data eq "View" )
  129. {
  130.     substr($PATH, 0, 1) = "/~" ;
  131.     print <<ENDOFTEXT ;
  132. Location: $PATH
  133.  
  134. ENDOFTEXT
  135. }
  136. else
  137. {
  138.     print <<ENDOFTEXT ;
  139. Content-type: text/html
  140.  
  141. <HEAD><TITLE>404 Not Found</TITLE></HEAD>
  142. <BODY><H1>404 Not Found</H1>
  143. The requested URL $PATH was not found on this server.<P>
  144. </BODY>
  145. ENDOFTEXT
  146. }
  147.  
  148. #__________________________________________________________
  149.  
  150. sub ValidPath
  151. {
  152.     return 1 unless /\.\./ ;
  153.    
  154.     return '' if /^\.\./ ;
  155.     return '' if /\/\.\.\// ;
  156.     return '' if /\.\.$/ ;
  157.  
  158.     return 1 ;
  159. }
  160.  
  161. sub ErrBadPath
  162. {
  163.         print <<ENDOFTEXT ;
  164. Content-type: text/html
  165.  
  166. <HTML>
  167. <HEAD><TITLE>OutBox: File Not Found</TITLE></HEAD>
  168. <BODY><H2>OutBox: File Not Found</H2>
  169. The requested file "$PATH" was not found on this OutBox page.
  170. <P>
  171. ENDOFTEXT
  172.  
  173. if( defined $ENV{'HTTP_REFERER'} )
  174. {
  175.     $referer = $ENV{'HTTP_REFERER'} ;
  176.     print <<ENDOFTEXT ;
  177. <a href="$referer"><IMG SRC="/outbox/images/go-back.gif" BORDER=0 ALT="Back"></A>
  178. ENDOFTEXT
  179. }
  180.  
  181.         print <<ENDOFTEXT ;
  182. </BODY>
  183. </HTML>
  184. ENDOFTEXT
  185.  
  186.         exit ;
  187. }
  188.  
  189.